package pl.enigmatic.util;

/**
 * Set of time related utility methods
 * @author reggie_7
 */
public class Time {

	public static final int MINUTES_TO_SECONDS = 60;
	public static final int HOURS_TO_MINUTES = 60;
	public static final int HOURS_TO_SECONDS = HOURS_TO_MINUTES * MINUTES_TO_SECONDS;
	public static final int DAYS_TO_HOURS = 24;
	public static final int DAYS_TO_MINUTES = DAYS_TO_HOURS * HOURS_TO_MINUTES;
	public static final int DAYS_TO_SECONDS = DAYS_TO_HOURS * HOURS_TO_SECONDS;

	public static final double SECONDS_TO_MINUTES = 1.0 / MINUTES_TO_SECONDS;
	public static final double MINUTES_TO_HOURS = 1.0 / HOURS_TO_MINUTES;
	public static final double SECONDS_TO_HOURS = 1.0 / HOURS_TO_SECONDS;
	public static final double HOURS_TO_DAYS = 1.0 / DAYS_TO_HOURS;
	public static final double MINUTES_TO_DAYS = 1.0 / DAYS_TO_MINUTES;
	public static final double SECONDS_TO_DAYS = 1.0 / DAYS_TO_SECONDS;

	private Time() {}

	public static double nanoToSeconds(final long nanoseconds) {
		return nanoseconds * Metrics.NANO;
	}

	public static long secondsToNano(final double seconds) {
		return (long) (seconds * Metrics.NANO_INV);
	}

	public static double milliToSeconds(final long milliseconds) {
		return milliseconds * Metrics.MILLI;
	}

	public static long secondsToMilli(final double seconds) {
		return (long) (seconds * Metrics.MILLI_INV);
	}

	public static double currentTimeSecondsNano() {
		return nanoToSeconds(System.nanoTime());
	}

	public static double currentTimeSecondsMilli() {
		return nanoToSeconds(System.currentTimeMillis());
	}

	public static double secondsElapsedMilli(final long millisecondsTimestampFrom) {
		return milliToSeconds(System.currentTimeMillis() - millisecondsTimestampFrom);
	}

	public static double secondsElapsedNano(final long nanosecondsTimestampFrom) {
		return milliToSeconds(System.nanoTime() - nanosecondsTimestampFrom);
	}

	public static int toSeconds(final int days, final int hours, final int minutes, final int seconds) {
		return seconds + MINUTES_TO_SECONDS * minutes + HOURS_TO_SECONDS * hours + DAYS_TO_SECONDS * days;
	}

}
